# Required vars
# - openqa_workers
##      integer - number of worker instances to create/run

# Required vars with defaults
# - openqa_hostname
##      string - hostname of openQA server to run jobs for
##      default - localhost

# Optional vars
# - openqa_tap
##      bool - whether this is the tap-enabled host or not
##             each deployment should have *one* tap-capable worker host

- name: Install required packages (testing)
  dnf: name={{ item }} state=present enablerepo="updates-testing"
  with_items:
  - openqa-worker
  tags:
  - packages

- name: Install packages
  dnf: name={{ item }} state=present
  with_items:
  - libselinux-python
  - git
  tags:
  - packages

- name: Install UEFI firmware package (x86_64 only)
  dnf: name=edk2-ovmf state=present
  tags:
  - packages
  when: "ansible_architecture is defined and ansible_architecture == 'x86_64'"

- name: Install boot script (to set /dev/kvm perms and disable SMT) (ppc64 only)
  copy: src=openqa-ppc64-prep.sh dest=/usr/local/sbin/openqa-ppc64-prep.sh owner=root group=root mode=0755
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"

- name: Install systemd service to run boot script (ppc64 only)
  copy: src=openqa-ppc64-prep.service dest=/etc/systemd/system/openqa-ppc64-prep.service
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"
  register: prepservice

- name: Do systemctl daemon-reload to register new service (ppc64 only)
  command: "systemctl daemon-reload"
  when: "prepservice is defined and prepservice|changed"

- name: Enable systemd service to run boot script (ppc64 only)
  service: name=openqa-ppc64-prep enabled=yes state=started
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"

- import_tasks: nfs-client.yml
  when: openqa_hostname is defined and openqa_hostname != "localhost"

- import_tasks: tap-setup.yml
  when: openqa_tap is defined and openqa_tap

- name: openQA client config
  template: src=client.conf.j2 dest=/etc/openqa/client.conf owner=_openqa-worker group=root mode=0600
  tags:
  - config

- name: openQA worker config
  template: src=workers.ini.j2 dest=/etc/openqa/workers.ini owner=_openqa-worker group=root mode=0644
  tags:
  - config

- import_tasks: createhdds.yml
  when: "inventory_hostname in groups['openqa-hdds-workers']"

- name: Enable and start worker services
  service: name=openqa-worker@{{ item }} enabled=yes state=started
  with_sequence: "count={{ openqa_workers }}"
